package admin.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import admin.model.Admin;

import viewmodel.UserEditStatus;
import viewmodel.VideoEditStatus;

import model.Connect;
import model.Video;

public class VideoDAO {
	
	public static List<Video> getAllVideo(){
		ArrayList<Video> listVideo = new ArrayList<Video>();
		
		String sql = "SELECT * FROM eonl_video ORDER BY id DESC";
		
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				Video user = new Video(result.getInt("id"), result.getString("name"), result.getString("image"), result.getString("username"), result.getString("video"));
				listVideo.add(user);
				
			}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return listVideo;
	}

	public static void removeVideo(Video video) {
		String sql = "DELETE FROM `eonl_video` WHERE id=" + video.getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public static void updateVideo(VideoEditStatus ves) {
		
		String sql ="UPDATE `eonl_video`"+" SET name = '"+ves.getVideo().getName()+"',username='"+ves.getVideo().getUsername()+"',image='"+ves.getType().getTypeCode()+"',video='"+ves.getVideo().getFile()+"' WHERE id="+ves.getVideo().getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}

	public static void addVideo(Video video) {
		String sql = "INSERT INTO `eonl_video` (name,username,image,video) VALUES('"+video.getName()+"','"+video.getUsername()+"','"+video.getType()+"','"+video.getFile()+"')";
		System.out.println(sql+"sql");
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}
}
